﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Business.Domain.Application;
using Edge.Common;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Edge.Common.Utils;
using Shop.Business.Domain.Customers;
using Shop.Business.Domain.Orders;
using SortDirection=Edge.Common.SortDirection;

namespace Shop.Web.Controls.Ajax
{
    public partial class OrdersHtmlContent : System.Web.UI.UserControl
    {
        [QueryParameter("oStatus", IsRequired = false)]
        public string orderStatus;

        private OrderStatus bindStatus;

        public OrderStatus BindStatus
        {
            get { return bindStatus; }
            set { bindStatus = value; }
        }

        protected FetchOptions fetchOptions;
        protected OrderFilter filter;
        private int _count;
        private Customer currentCustomer;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            WebParameterAttribute.SetValues(this, Request);
            FetchOptionForm fetchForm = new FetchOptionForm();
            WebParameterAttribute.SetValues(fetchForm, Request);
            filter = new OrderFilter();
            currentCustomer = Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object;
            Customer contact = currentCustomer;
            WebParameterAttribute.SetValues(filter, Request);
            if (filter.CustomerId != null && filter.CustomerId > 0)
            {
                contact = Locator.GetService<ICustomerManager>().Load(filter.CustomerId);
            }
            if (filter.CustomerId != -99)
                filter.Customer = contact;
            _count = Locator.GetService<IOrderManager>().Count(filter);
            if (_count > 0)
            {
//                int pages = AjaxPager.SetPager(_count, fetchForm);

//                if (fetchForm.Page > pages)
//                    fetchForm.Page = pages;

                fetchOptions = FetchOptions.All;//fetchForm.Parse();
                fetchOptions.SortExpression = "DeliveryDate";
                fetchOptions.SortDirection = SortDirection.Descending;
                ICollection<Order> list = Locator.GetService<IOrderManager>().Find(fetchOptions, filter);

                rprOrders.DataSource = list;
                rprOrders.DataBind();
            }

//            phResults.Visible = _count > 0;
//            phEmptyResults.Visible = _count <= 0;

            base.OnPreRender(e);
        }

        protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HtmlInputButton btnDelItem = (HtmlInputButton)e.Item.FindControl("btnDelItem");
            OrderItem item = (OrderItem) e.Item.DataItem;
            if (btnDelItem != null)
            {
                btnDelItem.Visible = currentCustomer.Role != CustomerRole.Buyer;
                btnDelItem.Attributes["onclick"]="DeleteOrderItem("+item.Id+")";
            }
            HtmlInputCheckBox chbSetDelivered = (HtmlInputCheckBox)e.Item.FindControl("chbSetDelivered");
            if (chbSetDelivered != null)
            {
                chbSetDelivered.Visible = currentCustomer.Role != CustomerRole.Buyer && item.Order.OrderStatus == OrderStatus.Preorder;
                //chbSetDelivered.Attributes["onclick"] = "SetOrderItemDelivered(" + item.Id + ")";
            }
        }

        protected void rprOrders_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HtmlGenericControl btnDelivered = (HtmlGenericControl)e.Item.FindControl("btnDelivered");
            HtmlInputButton btnDelOrder = (HtmlInputButton)e.Item.FindControl("btnDelOrder");
            Order item = (Order)e.Item.DataItem;
            if (btnDelivered != null)
            {
                btnDelivered.Visible = currentCustomer.Role != CustomerRole.Buyer && (item.OrderStatus == OrderStatus.Current || item.OrderStatus == OrderStatus.Preorder);
                btnDelivered.Attributes["onclick"] = "SetOrderDelivered(" + item.Id + ")";
            }
            if (btnDelOrder != null)
            {
                btnDelOrder.Visible = currentCustomer.Role != CustomerRole.Buyer && item.OrderStatus == OrderStatus.Current;
                btnDelOrder.Attributes["onclick"] = "DeleteOrder(" + item.Id + ")";
            }

        }
    }
}